{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import predict_ner.predict_base as ner\n",
    "import predict_DM.predict_DM as DM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "penultimate_intent = []\n",
    "penultimate_entities =[] \n",
    "previous_intent = []\n",
    "previous_entities = []\n",
    "current_intent = []\n",
    "current_entities = []\n",
    "\n",
    "penultimate_action = []\n",
    "previous_action = []\n",
    "current_action = []\n",
    "\n",
    "transfer_intent = []\n",
    "transfer_entitites= []\n",
    "transfer_action = []"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- interpreter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "intent:Control-AC_Mode \n",
      " slot:{'device': '空调', 'mode': '制冷模式', 'address': '主卧'}\n"
     ]
    }
   ],
   "source": [
    "intent,entities,entities_dic = ner.predict('主卧空调调为制冷模式')\n",
    "entities = [key for key in slots.keys()]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- interpreter_tracker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "def entities_tracker(intent,entities,current_intent,current_entities,\\\n",
    "                     previous_intent,previous_entities,penultimate_intent,penultimate_entities):\n",
    "    if entities == []:\n",
    "        entities = ['PAD']pipei\n",
    "    penultimate_intent = previous_intent\n",
    "    penultimate_entities = previous_entities\n",
    "    print('penultimate_intent: ',penultimate_intent)\n",
    "    print('penultimate_entities: ', penultimate_entities)\n",
    "    \n",
    "    previous_intent = current_intent\n",
    "    previous_entities = current_entities \n",
    "    print('previous_intent: ',previous_intent)\n",
    "    print('previous_entities: ',previous_entities)\n",
    "\n",
    "    current_intent = intent\n",
    "    current_entities = entities\n",
    "    print('current_intent: ',current_intent)\n",
    "    print('current_entities: ',current_entities)\n",
    "    \n",
    "    transfer_intent = current_intent + previous_intent + penultimate_intent\n",
    "    if 'PAD' in transfer_intent:\n",
    "        for index, val in enumerate(transfer_intent):\n",
    "            if val == 'PAD':\n",
    "                transfer_intent.pop(index)\n",
    "         \n",
    "    transfer_entitites = current_entities + previous_entities + penultimate_entities\n",
    "    if 'PAD' in transfer_entitites:\n",
    "        for index, val in enumerate(transfer_entitites):\n",
    "            if val == 'PAD':\n",
    "                transfer_entitites.pop(index)\n",
    "        \n",
    "    print('transfer_intent : ',transfer_intent)\n",
    "    print('transfer_entitites : ',set(transfer_entitites))\n",
    "    return current_intent,current_entities,previous_intent,previous_entities,\\\n",
    "            penultimate_intent,penultimate_entities,list(set(transfer_entitites)), transfer_intent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "penultimate_intent:  []\n",
      "penultimate_entities:  []\n",
      "previous_intent:  ['Control-AC_Open']\n",
      "previous_entities:  ['address', 'device', 'operation']\n",
      "current_intent:  ['Control-AC_Mode']\n",
      "current_entities:  ['device', 'mode', 'address']\n",
      "transfer_intent :  ['Control-AC_Mode', 'Control-AC_Open']\n",
      "transfer_entitites :  {'device', 'mode', 'operation', 'address'}\n"
     ]
    }
   ],
   "source": [
    "current_intent,current_entities,previous_intent,previous_entities,penultimate_intent,penultimate_entities,transfer_entitites, transfer_intent=entities_tracker(intent,entities,current_intent,current_entities,\\\n",
    "                     previous_intent,previous_entities,penultimate_intent,penultimate_entities)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- policy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "tracker = [transfer_action,transfer_entitites,transfer_intent]\n",
    "if tracker[0] == []:\n",
    "    tracker.pop(0)\n",
    "    tracker.insert(0,['PAD'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['action_open_ac'],\n",
       " ['device', 'mode', 'operation', 'address'],\n",
       " ['Control-AC_Mode', 'Control-AC_Open']]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tracker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "　text: [['action_open_ac'], ['device', 'mode', 'operation', 'address'], ['Control-AC_Mode', 'Control-AC_Open']] \n",
      " action:acmode_form \n",
      " \n"
     ]
    }
   ],
   "source": [
    "action  = DM.predict(tracker)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- action_tracker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "def entities_tracker(action,current_action,previous_action,penultimate_action):\n",
    "    if action == []:\n",
    "        action = ['PAD']\n",
    "    penultimate_action = previous_action\n",
    "    print('penultimate_action: ', penultimate_action)\n",
    "    \n",
    "    previous_action = current_action\n",
    "    print('previous_action: ',previous_action)\n",
    "\n",
    "    current_action = [action]\n",
    "    print('current_action: ',current_action)\n",
    "    \n",
    "    transfer_action = current_action + previous_action + penultimate_action\n",
    "    if 'PAD' in transfer_action:\n",
    "        for index, val in enumerate(transfer_action):\n",
    "            if val == 'PAD':\n",
    "                transfer_action.pop(index)\n",
    "\n",
    "    print('transfer_action : ',transfer_action)\n",
    "    return current_action,previous_action,penultimate_action, transfer_action"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "penultimate_action:  []\n",
      "previous_action:  ['action_open_ac']\n",
      "current_action:  ['acmode_form']\n",
      "transfer_action :  ['acmode_form', 'action_open_ac']\n"
     ]
    }
   ],
   "source": [
    "current_action,previous_action,penultimate_action, transfer_action = entities_tracker(action,current_action,previous_action,penultimate_action)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "tracker = [transfer_action,transfer_entitites,transfer_intent]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[['acmode_form', 'action_open_ac'],\n",
       " ['device', 'mode', 'operation', 'address'],\n",
       " ['Control-AC_Mode', 'Control-AC_Open']]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tracker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
